import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class MaintainPeople extends PhotoServlet {

    String initpath;
    String subtitle, table, thispage, updatepage;
    String code1, code2, code3, code4, code5;
    String title1, title2, title3, title4, title5;

    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        subtitle="People";
        table="people";
        thispage="MaintainPeople";
        updatepage="UpdatePerson";
        code1="firstName";
        code2="middleName";
        code3="lastName";
        code4="maidenName";
        code5="suffix";
        title1="First Name";
        title2="Middle Name";
        title3="Last Name";
        title4="Maiden Name";
        title5="Suffix";
    }
        
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title>Maintain " + subtitle + "</title>");  
        out.println("</head>");
        out.println("<body>");

        String query;
        ResultSet rs;

        out.println("<FORM ACTION=\"" + updatepage + "\" METHOD=\"GET\">");

        query = "SELECT uid, " + code1 + "," + code2 + "," + code3 + "," + code4 + "," + code5 + " " +
                "FROM " + table + " " +
                "ORDER BY " + code3 + "," + code1 + "," + code2 + "," + code5;
            
        if(showQuery)
            out.println("Query:<BR>" + query + "<BR>");

        rs = executeQuery(query);

        out.println("<B>" + subtitle + "</B><BR>");
        out.println("<SELECT NAME=\"selection\" SIZE=\"20\">");

        try
        {
            while (rs.next()) 
            {
                String uid = rs.getString("uid");
                String value=MaintainPeople.FirstNameLastName(rs);

                out.print("<OPTION ");
                if(rs.isFirst())
                	out.print("SELECTED ");
                else
                	out.print("         ");
                out.print("VALUE=\"" + uid + "\">" + value);
                out.println();
            }
            out.println("</SELECT>");
            out.println("<INPUT TYPE=SUBMIT NAME=\"Update\" VALUE=\"Update\">");
			out.println("</FORM>");

            out.println("<FORM ACTION=\"" + thispage + "\" METHOD=\"POST\">");
            out.println("<BR><BR><TABLE>");
            out.println("<TR><TD>" + title1 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT1></TD></TR>");
            out.println("<TR><TD>" + title2 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT2></TD></TR>");
            out.println("<TR><TD>" + title3 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT3></TD></TR>");
            out.println("<TR><TD>" + title4 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT4></TD></TR>");
            out.println("<TR><TD>" + title5 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT5></TD></TR>");
            out.println("</TABLE>");
            out.println("<INPUT TYPE=SUBMIT NAME=\"Add\" VALUE=\"Add\">");
            out.println("Show Query <INPUT TYPE=CHECKBOX NAME=\"showQuery\">");
            out.println("</FORM>");

        }
        catch(SQLException sqle)
        {
            out.println("SQL Exception: "+sqle.toString());
        }
        
        out.println("</body>");
        out.println("</html>");

    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();
        
        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title>"+title+" Added</title>");  
        out.println("</head>");
        out.println("<body>");

        if(showQuery)
        {
            for(Enumeration e=request.getParameterNames(); e.hasMoreElements(); )
            {
                String currentElement=e.nextElement().toString();
                String currentValues[]=request.getParameterValues(currentElement);
                for(int i=0; i<currentValues.length; i++)
                    out.println(currentElement + "[" + i + "]=" + 
                            currentValues[i] + "<BR>");
            }
        }

        String query;
            
        String value1=request.getParameter("TEXTINPUT1");
        String value2=request.getParameter("TEXTINPUT2");
        String value3=request.getParameter("TEXTINPUT3");
        String value4=request.getParameter("TEXTINPUT4");
        String value5=request.getParameter("TEXTINPUT5");

        query = "INSERT INTO " + table + " " +
                "SET " + code1 + "=\"" + value1 + "\", " +
                         code2 + "=\"" + value2 + "\", " +
                         code3 + "=\"" + value3 + "\", " +
                         code4 + "=\"" + value4 + "\", " +
                         code5 + "=\"" + value5 + "\"";

        if(showQuery) out.println("Query: <BR>" + query + "<BR>");

        int count = executeUpdate(query);
        if(count > 0)
        {
           out.println("<H1>"+title+" Updated</H1>");
        }
        else
        {
           out.println("<H1>Error: Nothing updated</H1>");
        }

        out.println("<A HREF=" + thispage + "><H3>Return to Previous Page</H3></A>");


        out.println("</BODY></HTML>");
    }
               
}



